
package DAO;

import Utils.HibernateUtil;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import model.Metric_User;
import model.User;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import validator.e;

public class MetricaDA extends BaseDAO<Metric_User>{

    @Override
    public void save(Metric_User metric) {
     
     Session session = HibernateUtil.getSessionFactory().openSession(); 
     Transaction t = session.beginTransaction();
     
     if(consult(metric.getName())){
     try{
         session.save(metric);
         t.commit();
         e.exibirMsgSucesso("Sucess");
     }catch(HibernateException err){
         e.exibirMsgErro("Error - Try Again");
     }}else{
         Query query = session.createQuery("delete from Metric_User");
         query.executeUpdate();
         session.save(metric);
         t.commit();
         e.exibirMsgSucesso("Sucess");
     }
     session.close();
    }

    public List<Metric_User> glist(int id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        Query query= session.createSQLQuery("select * from Metric_User where category_id = :category_id").addEntity(Metric_User.class);
        query.setParameter("category_id", id);
        List lista = query.list();
        session.close();
        return lista;
    }

    @Override
    public void update(Metric_User metric) {
    Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        try{
            session.update(metric);
            t.commit();
            e.exibirMsgSucesso("Sucess");
        }
        catch(HibernateException err){
            e.exibirMsgErro("Error");
        }
        session.close();
    }

    @Override
    public void remove(Metric_User metric) {
         Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        session.delete(metric);
        t.commit();
        session.close();
    }

    @Override
    public boolean consult(String name) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        Query query= session.createSQLQuery("select * from metric_user where name = :name").addEntity(Metric_User.class);
        query.setParameter("name",name);
        List lista = query.list();
        if(lista == null || lista.isEmpty()){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<Metric_User> list() {
    FacesContext fc = FacesContext.getCurrentInstance();
    HttpSession sessions = (HttpSession) fc.getExternalContext().getSession(false);
    User u = (User) sessions.getAttribute("user");    
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction t = session.beginTransaction();
    Query query= session.createSQLQuery("select * from Metric_User where user= :user_id").addEntity(Metric_User.class);
    query.setParameter("user_id", u.getId());
    List lista = query.list();
    session.close();
    return lista;
    }
    
}
